package com.pioneer.gui.invoice.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

import java.util.Vector;
//import java.util.Collection;

public class DropDownDataFactory
implements Constants
{
	private final String BILL="BILL";
	private final String SHIP="SHIP";
	
	String whereCondition="";
	
    public DropDownDataFactory()
    {    	
    }
    
    public DropDownDataFactory(String whereCondition)
    {
    	this.whereCondition=whereCondition;
    }
    
    public Vector getVector(String constant)
    {
    	if (constant.equals(INVOICE_TYPE)) 
    		return getInvoiceTypes();
    	else if (constant.equals(ENTRY_STATUS)) 
    		return getEntryStatusTypes();
    	else if (constant.equals(SALESMAN_LIST))
    		return getSalesmanList();
    	else if (constant.equals(CUSTOMER_LIST))
    		return getCustomerList();
    	else if (constant.equals(INVOICE_NO))
    		return getInvoiceNo();
    	else if (constant.equals(ITEM_NO))
    		return getItemNo();
    	else if (constant.equals(ITEM_TYPE))
    		return getItemType();
    	else if (constant.equals(QTY_TYPE))
    		return getQtyType();
    	else if (constant.equals(BILLING_LIST))
    		return getAddressList(BILL);
    	else if (constant.equals(SHIPPING_LIST))
    		return getAddressList(SHIP);
    	else
    		return new Vector();    		
    }
    
    private Vector getInvoiceTypes()
    {    	
    	Vector values=new Vector();
    	Connection conn=null;
    	Statement stat=null;
		ResultSet rs=null;
		
		ConnectionServer connServer=ConnectionServer.getSingleton();
		try
		{
			conn=connServer.getConnection();
			stat=conn.createStatement();
			rs=stat.executeQuery("select * from d04_invoice_types");
			Object[] dropDownValue;
			while (rs.next())
			{
				dropDownValue=new Object[2];
				dropDownValue[0]=rs.getString("d04_type");
				dropDownValue[1]=rs.getString("d04_desc");
				values.add(dropDownValue);				
			}
		}
		catch (SQLException e)
		{
			System.out.println(e.toString());
		}
		finally
		{
			try
			{
				if (rs!=null) rs.close();
				if (stat!=null) stat.close();
			}
			catch (SQLException e)
			{
				System.out.println(e.toString());
			}
		}    	
    	
    	return values;      	
    }
    
    private Vector getEntryStatusTypes()
    {    	
    	Vector values=new Vector();
    	Connection conn=null;
    	Statement stat=null;
		ResultSet rs=null;
		
		ConnectionServer connServer=ConnectionServer.getSingleton();
		try
		{
			conn=connServer.getConnection();
			stat=conn.createStatement();
			rs=stat.executeQuery("select * from d03_entry_status_types");
			Object[] dropDownValue;
			while (rs.next())
			{
				dropDownValue=new Object[2];
				dropDownValue[0]=rs.getString("d03_entry_status");
				dropDownValue[1]=rs.getString("d03_desc");
				values.add(dropDownValue);				
			}
		}
		catch (SQLException e)
		{
			System.out.println(e.toString());
		}
		finally
		{
			try
			{
				if (rs!=null) rs.close();
				if (stat!=null) stat.close();
			}
			catch (SQLException e)
			{
				System.out.println(e.toString());
			}
		}    	
    	
    	return values;      	
    }
    
    private Vector getQtyType()
    {    	
    	Vector values=new Vector();
    	Connection conn=null;
    	Statement stat=null;
		ResultSet rs=null;
		
		ConnectionServer connServer=ConnectionServer.getSingleton();
		try
		{
			conn=connServer.getConnection();
			stat=conn.createStatement();
			rs=stat.executeQuery("select * from d05_qty_types");
			Object[] dropDownValue;
			while (rs.next())
			{
				dropDownValue=new Object[2];
				dropDownValue[0]=rs.getString("d05_qty_type");
				dropDownValue[1]=rs.getString("d05_qty_type_desc");
				values.add(dropDownValue);				
			}
		}
		catch (SQLException e)
		{
			System.out.println(e.toString());
		}
		finally
		{
			try
			{
				if (rs!=null) rs.close();
				if (stat!=null) stat.close();
			}
			catch (SQLException e)
			{
				System.out.println(e.toString());
			}
		}    	
    	
    	return values;     	
    }
    
    private Vector getItemType()
    {    	
    	Vector values=new Vector();
    	Connection conn=null;
    	Statement stat=null;
		ResultSet rs=null;
		
		ConnectionServer connServer=ConnectionServer.getSingleton();
		try
		{
			conn=connServer.getConnection();
			stat=conn.createStatement();
			rs=stat.executeQuery("select * from d06_item_types");
			Object[] dropDownValue;
			while (rs.next())
			{
				dropDownValue=new Object[2];
				dropDownValue[0]=rs.getString("d06_item_type");
				dropDownValue[1]=rs.getString("d06_item_type_desc");
				values.add(dropDownValue);				
			}
		}
		catch (SQLException e)
		{
			System.out.println(e.toString());
		}
		finally
		{
			try
			{
				if (rs!=null) rs.close();
				if (stat!=null) stat.close();
			}
			catch (SQLException e)
			{
				System.out.println(e.toString());
			}
		}    	
    	
    	return values;     	
    }
    
    private Vector getItemNo()
    {    	
    	Vector values=new Vector();
    	Connection conn=null;
    	Statement stat=null;
		ResultSet rs=null;
		
		ConnectionServer connServer=ConnectionServer.getSingleton();
		try
		{
			conn=connServer.getConnection();
			stat=conn.createStatement();
			rs=stat.executeQuery("select * from b01_item");
			Object[] dropDownValue;
			while (rs.next())
			{
				dropDownValue=new Object[2];
				dropDownValue[0]=rs.getString("b01_item_no");
				dropDownValue[1]=rs.getString("b01_item_no");
				values.add(dropDownValue);				
			}
		}
		catch (SQLException e)
		{
			System.out.println(e.toString());
		}
		finally
		{
			try
			{
				if (rs!=null) rs.close();
				if (stat!=null) stat.close();
			}
			catch (SQLException e)
			{
				System.out.println(e.toString());
			}
		}    	
    	
    	return values;      	
    }
    
    private Vector buildConstantsVector(Object[][] obj)
    {
    	Vector types=new Vector();
    	for (int i=0;i<obj.length;i++)
    	{
    		types.add(obj[i]);
    	}    	
    	return types;
    }
    
    private Vector getInvoiceNo()
    {

    	Vector values=new Vector();
    	Connection conn=null;
    	Statement stat=null;
		ResultSet rs=null;
		
		ConnectionServer connServer=ConnectionServer.getSingleton();
		try
		{
			conn=connServer.getConnection();
			stat=conn.createStatement();
			String sqlString;
			if (this.whereCondition=="")
				sqlString="select d01_invoice_id from d01_invoice_header";
			else
				sqlString="select d01_invoice_id from d01_invoice_header where d01_cus_id='"+this.whereCondition+"'";
			rs=stat.executeQuery(sqlString);
			Object[] dropDownValue;
			while (rs.next())
			{
				dropDownValue=new Object[2];
				
				dropDownValue[0]=rs.getString("d01_invoice_id");
				dropDownValue[1]=rs.getString("d01_invoice_id");
				values.add(dropDownValue);				
			}
		}
		catch (SQLException e)
		{
			System.out.println(e.toString());
		}
		finally
		{
			try
			{
				if (rs!=null) rs.close();
				if (stat!=null) stat.close();
			}
			catch (SQLException e)
			{
				System.out.println(e.toString());
			}
		}    	
    	
    	return values;    	
    }
    
    
    private Vector getSalesmanList()
    {

    	Vector values=new Vector();
    	Connection conn=null;
    	Statement stat=null;
		ResultSet rs=null;
		
		ConnectionServer connServer=ConnectionServer.getSingleton();
		try
		{
			conn=connServer.getConnection();
			stat=conn.createStatement();
			rs=stat.executeQuery("select * from a01_salesman");
			Object[] dropDownValue;
			while (rs.next())
			{
				dropDownValue=new Object[2];
				dropDownValue[0]=new Integer(rs.getInt("a01_sman_id"));
				dropDownValue[1]=rs.getString("a01_name");
				values.add(dropDownValue);				
			}
		}
		catch (SQLException e)
		{
			System.out.println(e.toString());
		}
		finally
		{
			try
			{
				if (rs!=null) rs.close();
				if (stat!=null) stat.close();
			}
			catch (SQLException e)
			{
				System.out.println(e.toString());
			}
		}    	
    	
    	return values;    	
    }
        
    private Vector getCustomerList()
    {    	
    	Vector values=new Vector();
    	
    	Connection conn=null;
    	Statement stat=null;
		ResultSet rs=null;
		
		ConnectionServer connServer=ConnectionServer.getSingleton();
		try
		{
			conn=connServer.getConnection();
			stat=conn.createStatement();
			rs=stat.executeQuery("select * from c01_customer");
			Object[] dropDownValue;
			dropDownValue=new Object[2];
			dropDownValue[0]=new Integer(0);
			dropDownValue[1]="All Customers";
			values.add(dropDownValue);
			
			while (rs.next())
			{
				dropDownValue=new Object[2];
				dropDownValue[0]=new Integer(rs.getInt("c01_cus_id"));
				dropDownValue[1]=rs.getString("c01_name");				
				values.add(dropDownValue);				
			}
		}
		catch (SQLException e)
		{
			System.out.println(e.toString());
		}
		finally
		{
			try
			{
				if (rs!=null) rs.close();
				if (stat!=null) stat.close();
			}
			catch (SQLException e)
			{
				System.out.println(e.toString());
			}
		}    	
    	return values;    	
    }    
    
    private Vector getAddressList(String type)
    {    	
    	Vector values=new Vector();
    	Connection conn=null;
    	Statement stat=null;
		ResultSet rs=null;
		
		ConnectionServer connServer=ConnectionServer.getSingleton();
		try
		{
			conn=connServer.getConnection();
			stat=conn.createStatement();
			rs=stat.executeQuery("select * from c02_contact where c02_cont_type='"+
			type+"' and c02_cus_id='"+whereCondition+"'");
			Object[] dropDownValue;
			while (rs.next())
			{
				dropDownValue=new Object[2];
				dropDownValue[0]=new Integer( rs.getInt("c02_cont_seq"));
				dropDownValue[1]=new String("Contact #"+String.valueOf( rs.getInt("c02_cont_seq")));
				values.add(dropDownValue);				
			}
		}
		catch (SQLException e)
		{
			System.out.println(e.toString());
		}
		finally
		{
			try
			{
				if (rs!=null) rs.close();
				if (stat!=null) stat.close();
			}
			catch (SQLException e)
			{
				System.out.println(e.toString());
			}
		}    	
    	
    	return values;      	
    }
}
